<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="24">
            <a-form-model-item label="所在年级" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="classId">
              <j-dict-select-tag @change='changeTest' v-model='model.gradeId' placeholder='请选择年级'
                                 dictCode='cou_grade,grade_name,id,tenant_id  is not null order by  grade_order' />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="所在班级" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="classId">
              <j-dict-select-tag placeholder='请选择班级'
                                 v-model='model.classId'
                                 :dictCode='vDict' />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="学生" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="studentsId">
              <JSelectMultiStudent v-model="model.studentId" :multiple='multiple' :query-config="selectUserQueryConfig"/>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="学年" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="teacherId">
              <j-dict-select-tag v-model='model.schoolYearId' placeholder='请选择学年'
                                 dictCode='cou_school_year,school_year_name,id' />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="学期" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="teacherId">
              <j-dict-select-tag v-model='model.semesterId' placeholder='请选择学期'
                                 dictCode='cou_semester,semester_name,id' />
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>

import { httpAction, getAction } from '@/api/manage'
import { getNoClassStudent, getAllClass } from '@/api/commonAPI'
import JSelectMultiStudent from '../../../components/public/JSelectMultiStudent'

export default {
  name: 'RelClassStudentForm',
  components: {JSelectMultiStudent},
  props: {
    //表单禁用
    disabled: {
      type: Boolean,
      default: false,
      required: false
    }
  },
  data() {
    return {
      model: {},
      multiple: false,
      classesList: [],
      studentsList: [],
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 }
      },
      // 选择用户查询条件配置
      selectUserQueryConfig: [
        // {key: 'phone', label: '电话'},
      ],
      confirmLoading: false,
      validatorRules: {},
      vDict: 'cou_class,class_name,id,grade_code = null order by class_order',
      url: {
        add: '/rel_class_student/relClassStudent/add',
        edit: '/rel_class_student/relClassStudent/edit',
        queryById: '/rel_class_student/relClassStudent/queryById'
      }
    }
  },
  computed: {
    formDisabled() {
      return this.disabled
    }
  },
  created() {
    //备份model原始值
    this.modelDefault = JSON.parse(JSON.stringify(this.model))
  },
  methods: {
    add() {
      this.edit(this.modelDefault)
    },
    edit(record) {
      this.model = Object.assign({}, record)
      if(this.model){
        this.model.gradeId = this.model.classId_dictText;
        this.changeTest();
      }
      this.visible = true
    },
    submitForm() {
      const that = this
      // 触发表单验证
      this.$refs.form.validate(valid => {
        if (valid) {
          that.confirmLoading = true
          let httpurl = ''
          let method = ''
          if (!this.model.id) {
            httpurl += this.url.add
            method = 'post'
          } else {
            httpurl += this.url.edit
            method = 'put'
          }
          httpAction(httpurl, this.model, method).then((res) => {
            if (res.success) {
              that.$message.success(res.message)
              that.$emit('ok')
            } else {
              that.$message.warning(res.message)
            }
          }).finally(() => {
            that.confirmLoading = false
          })
        }

      })
    },
    changeTest() {
      if (this.model.gradeId != null && this.model.gradeId != '') {
        this.vDict = 'cou_class,class_name,id,grade_code =' + this.model.gradeId + ' order by class_order'
      }
    }
  }
}
</script>